// Copyright (C) 1953-2022 NUDT
// Verilog module name - mbus_hub 
// Version: V4.1.0.20221212
// Created:
//         by - fenglin 
////////////////////////////////////////////////////////////////////////////
// Description:
//         
///////////////////////////////////////////////////////////////////////////

`timescale 1ns / 1ps

module mbus_hub
(
        i_clk           ,
        i_rst_n         ,
       
        iv_command      ,
	    i_command_wr    ,
        ov_command_ack  ,
        o_command_ack_wr, 

        iv_command_ack_tse  ,
	    i_command_ack_wr_tse,
        ov_command_tse  ,
        o_command_wr_tse,

        iv_command_ack_tau  ,
	    i_command_ack_wr_tau,
        ov_command_tau  ,
        o_command_wr_tau, 

        iv_command_ack_frer  ,
	    i_command_ack_wr_frer,
        ov_command_frer      ,
        o_command_wr_frer    , 		

        iv_command_ack_osm_0  ,
	    i_command_ack_wr_osm_0,
        ov_command_osm_0  ,
        o_command_wr_osm_0,
        
        iv_command_ack_osm_1  ,
	    i_command_ack_wr_osm_1,
        ov_command_osm_1  ,
        o_command_wr_osm_1,

        iv_command_ack_osm_2  ,
	    i_command_ack_wr_osm_2,
        ov_command_osm_2  ,
        o_command_wr_osm_2, 

        iv_command_ack_osm_3  ,
	    i_command_ack_wr_osm_3,
        ov_command_osm_3      ,
        o_command_wr_osm_3    ,

        iv_command_ack_osm_4  ,
	    i_command_ack_wr_osm_4,
        ov_command_osm_4  ,
        o_command_wr_osm_4, 

        iv_command_ack_osm_5  ,
	    i_command_ack_wr_osm_5,
        ov_command_osm_5  ,
        o_command_wr_osm_5, 

        iv_command_ack_osm_6  ,
	    i_command_ack_wr_osm_6,
        ov_command_osm_6  ,
        o_command_wr_osm_6, 

        iv_command_ack_osm_7  ,
	    i_command_ack_wr_osm_7,
        ov_command_osm_7  ,
        o_command_wr_osm_7     
);
// I/O
// i_clk & rst
input                  i_clk                  ;
input                  i_rst_n                ;      
//nmac data
input      [65:0]      iv_command             ;               
input                  i_command_wr           ;
output reg [65:0]      ov_command_ack         ;
output reg             o_command_ack_wr       ;
             
input      [63:0]      iv_command_ack_tse         ;               
input                  i_command_ack_wr_tse       ;
output reg [63:0]      ov_command_tse         ;
output reg             o_command_wr_tse       ;

input      [63:0]      iv_command_ack_tau         ;               
input                  i_command_ack_wr_tau       ;
output reg [63:0]      ov_command_tau         ;
output reg             o_command_wr_tau       ;

input      [63:0]      iv_command_ack_frer    ;
input                  i_command_ack_wr_frer  ;
output reg [63:0]      ov_command_frer        ;
output reg             o_command_wr_frer      ;

input      [63:0]      iv_command_ack_osm_0       ;
input                  i_command_ack_wr_osm_0     ;
output reg [63:0]      ov_command_osm_0       ;    
output reg             o_command_wr_osm_0     ;
                       
input      [63:0]      iv_command_ack_osm_1       ;
input                  i_command_ack_wr_osm_1     ;
output reg [63:0]      ov_command_osm_1       ;
output reg             o_command_wr_osm_1     ;
                                         
input      [63:0]      iv_command_ack_osm_2       ;
input                  i_command_ack_wr_osm_2     ;
output reg [63:0]      ov_command_osm_2       ;
output reg             o_command_wr_osm_2     ;
                                            
input      [63:0]      iv_command_ack_osm_3       ;
input                  i_command_ack_wr_osm_3     ;
output reg [63:0]      ov_command_osm_3       ;
output reg             o_command_wr_osm_3     ;
                                           
input      [63:0]      iv_command_ack_osm_4       ;
input                  i_command_ack_wr_osm_4     ;
output reg [63:0]      ov_command_osm_4       ;
output reg             o_command_wr_osm_4     ;
                                           
input      [63:0]      iv_command_ack_osm_5       ;
input                  i_command_ack_wr_osm_5     ;
output reg [63:0]      ov_command_osm_5       ;
output reg             o_command_wr_osm_5     ;
                                          
input      [63:0]      iv_command_ack_osm_6       ;
input                  i_command_ack_wr_osm_6     ;
output reg [63:0]      ov_command_osm_6       ;
output reg             o_command_wr_osm_6     ;
                                           
input      [63:0]      iv_command_ack_osm_7       ;
input                  i_command_ack_wr_osm_7     ;
output reg [63:0]      ov_command_osm_7       ;
output reg             o_command_wr_osm_7     ;
//***************************************************
//               command parse
//***************************************************
always @(posedge i_clk or negedge i_rst_n) begin
    if(i_rst_n == 1'b0)begin
        ov_command_tse        <= 64'b0;
        o_command_wr_tse      <= 1'b0;    
                              
        ov_command_tau        <= 64'b0;
        o_command_wr_tau      <= 1'b0;
		
		ov_command_frer       <= 64'b0;
        o_command_wr_frer     <= 1'b0;

        ov_command_osm_0      <= 64'b0;
        o_command_wr_osm_0    <= 1'b0;
        
        ov_command_osm_1      <= 64'b0;
        o_command_wr_osm_1    <= 1'b0; 

        ov_command_osm_2      <= 64'b0;
        o_command_wr_osm_2    <= 1'b0;   

        ov_command_osm_3      <= 64'b0;
        o_command_wr_osm_3    <= 1'b0;   

        ov_command_osm_4      <= 64'b0;
        o_command_wr_osm_4    <= 1'b0;   

        ov_command_osm_5      <= 64'b0;
        o_command_wr_osm_5    <= 1'b0;   

        ov_command_osm_6      <= 64'b0;
        o_command_wr_osm_6    <= 1'b0;   

        ov_command_osm_7      <= 64'b0;
        o_command_wr_osm_7    <= 1'b0;          
    end
    else begin
        if(i_command_wr)begin
            if(iv_command[63:62] == 2'd1)begin//tss/tse
                ov_command_tse[63:62]        <= iv_command[65:64];            
                ov_command_tse[61:58]        <= iv_command[61:58];
                ov_command_tse[57:0]         <= iv_command[57:0] ;
                o_command_wr_tse             <= 1'b1;

                o_command_wr_osm_0             <= 1'b0; 
                o_command_wr_osm_1             <= 1'b0; 
                o_command_wr_osm_2             <= 1'b0; 
                o_command_wr_osm_3             <= 1'b0; 
                o_command_wr_osm_4             <= 1'b0; 
                o_command_wr_osm_5             <= 1'b0; 
                o_command_wr_osm_6             <= 1'b0; 
                o_command_wr_osm_7             <= 1'b0;

                o_command_wr_tau               <= 1'b0;
                o_command_wr_frer              <= 1'b0;				
            end
            else if(iv_command[63:62] == 2'd2)begin//osm     
                if(iv_command[57:51] == 7'd0)begin//osm_0
                    ov_command_osm_0[63:62]        <= iv_command[65:64];            
                    ov_command_osm_0[61:58]        <= iv_command[61:58];
                    ov_command_osm_0[57:0]         <= iv_command[57:0] ;                
                    
                    o_command_wr_osm_0             <= 1'b1; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end
                else if(iv_command[57:51] == 7'd1)begin//osm_1
                    ov_command_osm_1[63:62]        <= iv_command[65:64];            
                    ov_command_osm_1[61:58]        <= iv_command[61:58];
                    ov_command_osm_1[57:0]         <= iv_command[57:0] ;  
 
                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b1; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0; 
                    o_command_wr_frer              <= 1'b0;					
                end
                else if(iv_command[57:51] == 7'd2)begin//osm_2
                    ov_command_osm_2[63:62]        <= iv_command[65:64];            
                    ov_command_osm_2[61:58]        <= iv_command[61:58];
                    ov_command_osm_2[57:0]         <= iv_command[57:0] ;                  

                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b1; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end
                else if(iv_command[57:51] == 7'd3)begin//osm_3
                    ov_command_osm_3[63:62]        <= iv_command[65:64];            
                    ov_command_osm_3[61:58]        <= iv_command[61:58];
                    ov_command_osm_3[57:0]         <= iv_command[57:0] ;  
 
                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b1; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end
                else if(iv_command[57:51] == 7'd4)begin//osm_4
                    ov_command_osm_4[63:62]        <= iv_command[65:64];            
                    ov_command_osm_4[61:58]        <= iv_command[61:58];
                    ov_command_osm_4[57:0]         <= iv_command[57:0] ;                  
 
                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b1; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end
                else if(iv_command[57:51] == 7'd5)begin//osm_5
                    ov_command_osm_5[63:62]        <= iv_command[65:64];            
                    ov_command_osm_5[61:58]        <= iv_command[61:58];
                    ov_command_osm_5[57:0]         <= iv_command[57:0] ;                 
 
                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b1; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0; 
                    o_command_wr_frer              <= 1'b0;					
                end
                else if(iv_command[57:51] == 7'd6)begin//osm_6
                    ov_command_osm_6[63:62]        <= iv_command[65:64];            
                    ov_command_osm_6[61:58]        <= iv_command[61:58];
                    ov_command_osm_6[57:0]         <= iv_command[57:0] ;                 
 
                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b1; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end
                else if(iv_command[57:51] == 7'd7)begin//osm_7
                    ov_command_osm_7[63:62]        <= iv_command[65:64];            
                    ov_command_osm_7[61:58]        <= iv_command[61:58];
                    ov_command_osm_7[57:0]         <= iv_command[57:0] ;                 

                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b1;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end
                else begin
                    o_command_wr_osm_0             <= 1'b0; 
                    o_command_wr_osm_1             <= 1'b0; 
                    o_command_wr_osm_2             <= 1'b0; 
                    o_command_wr_osm_3             <= 1'b0; 
                    o_command_wr_osm_4             <= 1'b0; 
                    o_command_wr_osm_5             <= 1'b0; 
                    o_command_wr_osm_6             <= 1'b0; 
                    o_command_wr_osm_7             <= 1'b0;
                    
                    o_command_wr_tse               <= 1'b0;
                    o_command_wr_tau               <= 1'b0;
                    o_command_wr_frer              <= 1'b0;                    
                end                 
            end
            else if(iv_command[63:62] == 2'd3)begin//other            
                o_command_wr_osm_0             <= 1'b0; 
                o_command_wr_osm_1             <= 1'b0; 
                o_command_wr_osm_2             <= 1'b0; 
                o_command_wr_osm_3             <= 1'b0; 
                o_command_wr_osm_4             <= 1'b0; 
                o_command_wr_osm_5             <= 1'b0; 
                o_command_wr_osm_6             <= 1'b0; 
                o_command_wr_osm_7             <= 1'b0;
                                
                o_command_wr_tse               <= 1'b0;
				if(iv_command[61:58] == 4'd1)begin//tau
					ov_command_tau[63:62]          <= iv_command[65:64];            
					ov_command_tau[61:58]          <= iv_command[61:58];
					ov_command_tau[57:0]           <= iv_command[57:0] ; 
                    ov_command_frer                <= 64'b0            ;					
					
					o_command_wr_frer              <= 1'b0;
					o_command_wr_tau               <= 1'b1;
                end
                else if(iv_command[61:58] == 4'd0)begin//frer
					ov_command_frer[63:62]         <= iv_command[65:64];            
					ov_command_frer[61:58]         <= iv_command[61:58];
					ov_command_frer[57:0]          <= iv_command[57:0] ; 
                    ov_command_tau                 <= 64'b0            ;
					
                    o_command_wr_frer              <= 1'b1;
					o_command_wr_tau               <= 1'b0;
                end
                else begin
                    ov_command_frer                <= 64'b0            ;
					ov_command_tau                 <= 64'b0            ;
					
					o_command_wr_frer              <= 1'b0;
					o_command_wr_tau               <= 1'b0;
                end				
            end              
            else begin
                o_command_wr_osm_0             <= 1'b0; 
                o_command_wr_osm_1             <= 1'b0; 
                o_command_wr_osm_2             <= 1'b0; 
                o_command_wr_osm_3             <= 1'b0; 
                o_command_wr_osm_4             <= 1'b0; 
                o_command_wr_osm_5             <= 1'b0; 
                o_command_wr_osm_6             <= 1'b0; 
                o_command_wr_osm_7             <= 1'b0;
                
                o_command_wr_tse               <= 1'b0;
                o_command_wr_tau               <= 1'b0; 
                o_command_wr_frer              <= 1'b0;				
            end
        end      
        else begin
            o_command_wr_osm_0             <= 1'b0; 
            o_command_wr_osm_1             <= 1'b0; 
            o_command_wr_osm_2             <= 1'b0; 
            o_command_wr_osm_3             <= 1'b0; 
            o_command_wr_osm_4             <= 1'b0; 
            o_command_wr_osm_5             <= 1'b0; 
            o_command_wr_osm_6             <= 1'b0; 
            o_command_wr_osm_7             <= 1'b0;
            
            o_command_wr_tse               <= 1'b0;
            o_command_wr_tau               <= 1'b0;
            o_command_wr_frer              <= 1'b0;			
        end
    end
end
//***************************************************
//               command encapsulate
//***************************************************
always @(posedge i_clk or negedge i_rst_n) begin
    if(i_rst_n == 1'b0)begin
        ov_command_ack        <= 66'b0;
        o_command_ack_wr      <= 1'b0 ;    
    end
    else begin
        if(i_command_ack_wr_tse)begin
            ov_command_ack[65:64]        <= iv_command_ack_tse[63:62];
            ov_command_ack[63:62]        <= 2'b01 ;
            ov_command_ack[61:58]        <= iv_command_ack_tse[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_tse[57:0] ;
            o_command_ack_wr             <= 1'b1  ;                
        end      
        else if(i_command_ack_wr_osm_0)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_0[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_0[61:58]; 
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_0[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;             
        end
        else if(i_command_ack_wr_osm_1)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_1[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_1[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_1[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end
        else if(i_command_ack_wr_osm_2)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_2[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_2[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_2[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end 
        else if(i_command_ack_wr_osm_3)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_3[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_3[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_3[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end 
        else if(i_command_ack_wr_osm_4)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_4[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_4[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_4[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end 
        else if(i_command_ack_wr_osm_5)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_5[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_5[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_5[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end 
        else if(i_command_ack_wr_osm_6)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_6[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_6[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_6[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end 
        else if(i_command_ack_wr_osm_7)begin
            ov_command_ack[65:64]        <= iv_command_ack_osm_7[63:62];
            ov_command_ack[63:62]        <= 2'b10                      ;
            ov_command_ack[61:58]        <= iv_command_ack_osm_7[61:58];
            ov_command_ack[57:0 ]        <= iv_command_ack_osm_7[57:0] ;
            o_command_ack_wr             <= 1'b1                       ;
        end 
        else if(i_command_ack_wr_tau)begin
            ov_command_ack[65:64]        <= iv_command_ack_tau[63:62]; 
            ov_command_ack[63:62]        <= 2'b11                    ; 
            ov_command_ack[61:58]        <= iv_command_ack_tau[61:58]; 
            ov_command_ack[57:0 ]        <= iv_command_ack_tau[57:0] ; 
            o_command_ack_wr             <= 1'b1                     ; 
        end
        else if(i_command_ack_wr_frer)begin
            ov_command_ack[65:64]        <= iv_command_ack_frer[63:62]; 
            ov_command_ack[63:62]        <= 2'b11                    ; 
            ov_command_ack[61:58]        <= iv_command_ack_frer[61:58]; 
            ov_command_ack[57:0 ]        <= iv_command_ack_frer[57:0] ; 
            o_command_ack_wr             <= 1'b1                     ; 
        end 		
        else begin
            ov_command_ack        <= 66'b0;
            o_command_ack_wr      <= 1'b0 ;               
        end         
    end
end     
endmodule
    